minetest.register_chatcommand("landrush", {
	params = "",
	description = "Exibe todos os comando deste mod",
	privs = {},
	func = function(name, param)
		minetest.chat_send_player(name, "    ", false)
		minetest.chat_send_player(name, "############################################################################################", false)
		minetest.chat_send_player(name, "### LANDRUSH (em Portugues Brasileiro)                                                   ###", false)
		minetest.chat_send_player(name, "### Code license: GNU LGPL version 2                                                     ###", false)
		minetest.chat_send_player(name, "############################################################################################", false)
		minetest.chat_send_player(name, "   * /landowner", false)
		minetest.chat_send_player(name, "       => Exibe o nome do proprietario do atual pedaco de mapa.", false)
		minetest.chat_send_player(name, "   * /unclaim", false)
		minetest.chat_send_player(name, "       => Desagrega o atual pedaco de mapa.", false)
		minetest.chat_send_player(name, "   * /userunclaim <NomeDoPersonagem>", false)
		minetest.chat_send_player(name, "       => Desagrega todas os pedacos de mapa de um personagem especifico.", false)
		minetest.chat_send_player(name, "       (Necessita do privilegio 'landrush' para funcionar)", false)
		minetest.chat_send_player(name, "   * /sharearea <NomeDoPersonagem|all>", false)
		minetest.chat_send_player(name, "       => Adiciona permissao de construcao para todos ou alguem no atual pedaco de mapa.", false)
		minetest.chat_send_player(name, "   * /unsharearea <NomeDoPersonagem>", false)
		minetest.chat_send_player(name, "       => Remove permissao de construcao de alguem no atual pedaco de mapa.", false)
		minetest.chat_send_player(name, "   * /guests", false)
		minetest.chat_send_player(name, "       => Lista convidados para construir pelo proprietario do atual pedaco de mapa.", false)
		minetest.chat_send_player(name, "   * /showarea", false)
		minetest.chat_send_player(name, "       => Destaca os limites do atual pedaco de mapa.", false)
		minetest.chat_send_player(name, "   * /shareall <NomeDoPersonagem>", false)
		minetest.chat_send_player(name, "       => Adiciona permissao de todos seus pedacos de mapas para um alguem especifico.", false)
		minetest.chat_send_player(name, "   * /unshareall <NomeDoPersonagem>", false)
		minetest.chat_send_player(name, "       => Remone permissao de todos seus pedacos de mapas para um alguem especifico.", false)
		minetest.chat_send_player(name, "############################################################################################", false)
		minetest.chat_send_player(name, name..", precione F10 e use a rolagem do mouse para ler todo este tutorial!!!", false)
	end,
})

minetest.register_chatcommand("landowner", {
	params = "",
	description = "Exibe o nome do proprietário do atual pedaco de mapa",
	privs = {interact=true},
	func = function(name, param)
		local player = minetest.env:get_player_by_name(name)
		local pos = player:getpos()
		local owner = landrush.get_owner(pos)
		if owner then
			minetest.chat_send_player(name, "Esta area pertence a '"..owner.."'.")
		else
			minetest.chat_send_player(name, "Esta area nao tem proprietario.")
		end
	end,
})

minetest.register_chatcommand("userunclaim", {
	params = "player",
	privs = {landrush=true},
	description = "Desagrega todas os pedaco de mapa de um player especifico.",
	func = function(name, param)
		qdone = 0
		for k,v in pairs(landrush.claims) do
            if landrush.claims[k].owner == param then
                landrush.claims[k] = nil
                qdone = qdone + 1
            end
        end
        landrush.save_claims()
        minetest.chat_send_player(name,tostring(qdone).." pedacos de mapa foram desagregados de '"..param.."'.")
	end
})

minetest.register_chatcommand("unclaim", {
	params = "",
	description = "Desagrega o atual pedaco de mapa.",
	privs = {interact=true},
	func = function(name, param)
		local player = minetest.env:get_player_by_name(name)
		local pos = player:getpos()
		local owner = landrush.get_owner(pos)
		local inv = player:get_inventory()
		if owner then						
			if owner == name or minetest.check_player_privs(name, {landrush=true}) then
				chunk = landrush.get_chunk(pos)
					landrush.claims[chunk] = nil
					landrush.save_claims()
					minetest.chat_send_player(name, "Voce renunciou seu dominio sobre este pedaco de mapa.")
			else
				minetest.chat_send_player(name, "Esta area pertence a '"..owner.."'.")
			end
		else
			minetest.chat_send_player(name, "Esta area nao tem proprietario.")
		end
	end,
})

minetest.register_chatcommand("sharearea", {
	params = "<NomeDoPersonagem> ou 'all'",
	description = "Adiciona permissao de construcao para todos ou alguem em uma area de outro alguem.",
	privs = {interact=true},
	func = function(name, param)
		local player = minetest.env:get_player_by_name(name)
		local pos = player:getpos()
		local owner = landrush.get_owner(pos)
		if owner then
			if ( owner == name and name ~= param ) or minetest.check_player_privs(name, {landrush=true}) then
				if minetest.env:get_player_by_name(param) or param=="*all" then
					landrush.claims[landrush.get_chunk(pos)].shared[param] = param
					landrush.save_claims()
					minetest.chat_send_player(name, "'"..param.."' agora pode editar este pedaco de mapa.")
					minetest.chat_send_player(param, "'"..name.."' convidou voce para construir na area dele.")
				else
					minetest.chat_send_player(name, "'"..param.."' nao esta online, ou este nome esta incorreto.")
				end
			else
				minetest.chat_send_player(name, "Esta area pertence a '"..owner.."'.")
			end
		else
			minetest.chat_send_player(name, "Esta area nao tem proprietario.")
		end
	end,
})

minetest.register_chatcommand("unsharearea", {
	params = "<NomeDoPersonagem>",
	description = "unshares the current map chunk with <name>",
	privs = {interact=true},
	func = function(name, param)
		local player = minetest.env:get_player_by_name(name)
		local pos = player:getpos()
		local owner = landrush.get_owner(pos)
		if owner then
			if owner == name or minetest.check_player_privs(name, {landrush=true}) then
				if name ~= param then
					landrush.claims[landrush.get_chunk(pos)].shared[param] = nil
					landrush.save_claims()
					--minetest.chat_send_player(name, param.." may no longer edit this area.")
					--minetest.chat_send_player(param, name.." has just revoked your editing privileges in an area.")
					minetest.chat_send_player(name, "Agora '"..param.."' nao pode editar este pedaco de mapa.")
					minetest.chat_send_player(param, "'"..name.."' revogou seu convidou para construir na area dele.")
				else
					minetest.chat_send_player(name, 'Use o comando "/unclaim" para desagregar sua propria area.')
				end
			else
				minetest.chat_send_player(name, "Esta area pertence a '"..owner.."'.")
			end
		else
			minetest.chat_send_player(name, "Esta area nao tem proprietario.")
		end
	end,
})

minetest.register_chatcommand("guests", {
	params = "",
	description = "Lista convidados para construir pelo proprietario do atual pedaco de mapa.",
	privs = {interact=true},
	func = function(name, param)
		local player = minetest.env:get_player_by_name(name)
		local pos = player:getpos()
		local mayedit = landrush.get_owner(pos)
		if mayedit then
			local chunk = landrush.get_chunk(pos)
			for user, user in pairs(landrush.claims[chunk].shared) do
				mayedit = mayedit..", "..user
			end
			minetest.chat_send_player(name, mayedit)
		else
			minetest.chat_send_player(name,  "Esta area nao tem proprietario.")
		end
	end,
})

minetest.register_chatcommand("showarea", {
	params = "",
	description = "Destaca os limites da atual de área protegida.",
	privs = {interact=true},
	func = function(name, param)
		local player = minetest.env:get_player_by_name(name)
		local pos = player:getpos()		
				local entpos = landrush.get_chunk_center(pos)
				entpos.y = (pos.y-1)
				minetest.env:add_entity(entpos, "landrush:showarea")	
	end,
})

minetest.register_chatcommand("shareall", {
    params = "<NomeDoPersonagem>",
    description = "Adiciona permissao a todos seus pedacos de mapas para outro alguem especifico.",
    privs = {interact=true},
    func = function(name, param)
        
        if minetest.env:get_player_by_name(param) then
            local qdone = 0
            for k,v in pairs(landrush.claims) do
                if landrush.claims[k].owner == name then
                    landrush.claims[k].shared[param] = param
                    qdone = qdone + 1
                end
            end
        
            landrush.save_claims()
            
            if qdone > 0 then
                minetest.chat_send_player(name, param.." may now edit all of your areas.")
                minetest.chat_send_player(name, qdone.." total areas were shared.")
                minetest.chat_send_player(param, name.." has just shared all of their areas with you.")
            else
                minetest.chat_send_player(name, param.." was not given any permissions. You may not own any land.")
            end
        else
                minetest.chat_send_player(name, "'"..param.."' nao e um nome valido. O personagem precisa estar online para receber a permissao.")
        end
    end,
})

minetest.register_chatcommand("unshareall", {
    params = "<NomeDoPersonagem>",
    description = "Remove permissao a todos seus pedacos de mapas para outro alguem especifico.",
    privs = {interact=true},
    func = function(name, param)
        if name ~= param then
            local qdone = 0
            for k,v in pairs(landrush.claims) do
                if landrush.claims[k].owner == name then
                    landrush.claims[k].shared[param] = nil
                    qdone = qdone + 1
                end
            end
        
            landrush.save_claims()
            
            if qdone > 0 then
                minetest.chat_send_player(name, param.." may no longer edit any of your areas.")
                minetest.chat_send_player(name, qdone.." total areas were unshared.")
                minetest.chat_send_player(param, name.." has just unshared all of their areas with you.")
            else
                minetest.chat_send_player(name, param.." had no permissions being revoked. You may not own any land.")
            end
        else
            minetest.chat_send_player(name, 'Use "/unclaim" to unclaim any of your areas.')
        end
    end,
})
